# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=curl
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-winssl")
pkgver=7.87.0
pkgrel=3
pkgdesc="Command line tool and library for transferring data with URLs (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://curl.se/"
license=("spdx:MIT")
_cert_depends=("${MINGW_PACKAGE_PREFIX}-ca-certificates"
               "${MINGW_PACKAGE_PREFIX}-libssh2")
_openssl_depends=("${MINGW_PACKAGE_PREFIX}-openssl"
                  "${MINGW_PACKAGE_PREFIX}-nghttp2")
_gnutls_depends=("${MINGW_PACKAGE_PREFIX}-rtmpdump"
                 "${MINGW_PACKAGE_PREFIX}-gnutls")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${_cert_depends[@]}"
             "${_openssl_depends[@]}"
             "${_gnutls_depends[@]}")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-brotli"
         "${MINGW_PACKAGE_PREFIX}-libidn2"
         "${MINGW_PACKAGE_PREFIX}-libpsl"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
source=("https://github.com/curl/curl/releases/download/${_realname}-${pkgver//./_}/${_realname}-${pkgver}.tar.xz"{,.asc}
        "pathtools.c"
        "pathtools.h"
        "0001-Make-cURL-relocatable.patch"
        "0002-nghttp2-static.patch"
        "0003-libpsl-static-libs.patch"
        "0004-more-static-fixes.patch")
sha256sums=('ee5f1a1955b0ed413435ef79db28b834ea5f0fb7c8cfb1ce47175cc3bee08fff'
            'SKIP'
            '703cd0cb74e714f9e66d26de11c109dd76fab07e723af8dde56a35ea65102e5f'
            '4f9d325265ef6f4e90ad637dea41afa6995388c921fe961ad5dc895aca10318b'
            'c4e6bfd5b58f944d75293128effbd22fe42ee0131b915d9230ceb3c004c0322d'
            '3ee9c75a3046f86f91290c143170179230c9adc6eabfbb79eb26f708a165b719'
            '7492d019036b5bec251bfbc3c0b40e5f16d3dd6b2515068835e087a6c21f19ad'
            '590eb65e90e756eaad993d52a101f29091ada2c742c5a607684e88fc5c560d54')
validpgpkeys=('27EDEAF22F3ABCEB50DB9A125CC908FDB71E12C2')  # Daniel Stenberg

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd "${srcdir}/${_realname}-${pkgver}"
  cp -fHv "${srcdir}"/pathtools.[ch] lib/

  apply_patch_with_msg \
    0001-Make-cURL-relocatable.patch \
    0002-nghttp2-static.patch \
    0003-libpsl-static-libs.patch \
    0004-more-static-fixes.patch

  autoreconf -vfi
}

do_build() {
  _variant=$1
  _destdir="${srcdir}/build-${MSYSTEM}"
  if [ "${_variant}" != "-openssl" ]; then
    _destdir="${_destdir}${_variant}"
  fi
  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=( --enable-debug )
  else
    extra_config+=( --disable-debug )
  fi

  local -a _variant_config
  if [ "${_variant}" = "-winssl" ]; then
    _variant_config+=("--with-schannel")
    _variant_config+=('--without-nghttp2')
    _variant_config+=("--without-ca-bundle")
    _variant_config+=("--without-ca-path")
    _variant_config+=("--without-librtmp")
  elif [ "${_variant}" = "-gnutls" ]; then
    _variant_config+=("--with-default-ssl-backend=gnutls")
    _variant_config+=("--with-gnutls")
    _variant_config+=("--with-schannel")
    _variant_config+=('--without-nghttp2')
    _variant_config+=("--with-ca-bundle=${MINGW_PREFIX}/etc/ssl/certs/ca-bundle.crt")
    _variant_config+=("--with-librtmp")
  elif [ "${_variant}" = "-openssl" ]; then
    _variant_config+=("--with-default-ssl-backend=openssl")
    _variant_config+=("--with-openssl")
    _variant_config+=("--with-schannel")
    _variant_config+=("--with-ca-bundle=${MINGW_PREFIX}/etc/ssl/certs/ca-bundle.crt")
    _variant_config+=("--with-nghttp2=${MINGW_PREFIX}")
    _variant_config+=("--without-librtmp")
  fi

  msg2 "Building static library"
  mkdir -p "${_destdir}-static" && cd "${_destdir}-static"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-pthreads \
    --without-random \
    --enable-static \
    --disable-shared \
    --enable-sspi \
    --enable-ldap \
    --enable-ldaps \
    --with-brotli \
    --with-ldap-lib=wldap32 \
    --with-libssh2 \
    --with-zstd \
    "${_variant_config[@]}" \
    "${extra_config[@]}"
# there's a bug with zsh completion generation script and Windows.
# curl has to be specified with the file extension.
  sed -i "s|\/curl > \$\@|\/curl\$\{EXEEXT\} > \$\@|" scripts/Makefile
  make

  msg2 "Building shared library"
  mkdir -p "${_destdir}-shared" && cd "${_destdir}-shared"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-pthreads \
    --without-random \
    --disable-static \
    --enable-shared \
    --enable-sspi \
    --enable-ldap \
    --enable-ldaps \
    --with-brotli \
    --with-ldap-lib=wldap32 \
    --with-libssh2 \
    --with-zstd \
    "${_variant_config[@]}" \
    "${extra_config[@]}"
# there's a bug with zsh completion generation script and Windows.
# curl has to be specified with the file extension.
  sed -i "s|\/curl > \$\@|\/curl\$\{EXEEXT\} > \$\@|" scripts/Makefile
  make
}

build() {
  do_build -openssl
  do_build -winssl
  do_build -gnutls
}

do_package() {
  cd "${srcdir}/build-${MSYSTEM}$1-static"
  make DESTDIR="${pkgdir}" install

  cd "${srcdir}/build-${MSYSTEM}$1-shared"
  make DESTDIR="${pkgdir}" install

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/bin/curl-config
  sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/libcurl.pc

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}

package_curl() {
  depends=("${depends[@]}"
           "${_cert_depends[@]}"
           "${_openssl_depends[@]}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-winssl"
             "${MINGW_PACKAGE_PREFIX}-${_realname}-gnutls")
  do_package
}

package_curl-winssl() {
  depends=("${depends[@]}"
           "${MINGW_PACKAGE_PREFIX}-libssh2-wincng")
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}"
             "${MINGW_PACKAGE_PREFIX}-${_realname}-gnutls")
  do_package -winssl
}

package_curl-gnutls() {
  provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
  depends=("${depends[@]}"
           "${_cert_depends[@]}"
           "${_gnutls_depends[@]}")
  conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}"
             "${MINGW_PACKAGE_PREFIX}-${_realname}-winssl")
  do_package -gnutls
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
